package fdoj.eventInvitations.servlet;

import java.io.IOException;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import fdoj.eventInvitations.dao.InvantationDao;
import fdoj.eventInvitations.entity.Invantation;
import fdoj.eventInvitations.entity.Response;

public class RespondController extends HttpServlet{
	@EJB
	private InvantationDao invantionDao;
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			Invantation inv = (Invantation) request.getSession().getAttribute("answer_inv");
			String odgovor = request.getParameter("odgovor");
			if(odgovor.equals("prisustvujem")){
				inv.setStatus(Response.CONFIRMED);
			}else{
				inv.setStatus(Response.DECLINED);
			}
			invantionDao.merge(inv);
			response.sendRedirect(response.encodeRedirectURL("./index"));
			return;
		} catch (Exception e) {
			e.printStackTrace();

		}
		response.sendRedirect(response.encodeRedirectURL("./error.jsp"));
	}
}
